package com.ji.controller;

import com.ji.dao.DepartmentMapper;
import com.ji.dao.EmployeeMapper;
import com.ji.pojo.Department;
import com.ji.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;
import java.util.List;

@Controller
public class EployeeController {
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    DepartmentMapper departmentMapper;
    @RequestMapping("/emps")
    public String list(Model model){
        List<Employee> all = employeeMapper.queryAllEmployee ();
        model.addAttribute ("emps",all);
        return "emp/list";
    }
    @GetMapping("/emp")
    public String toAddpage(Model model){
        //查出所有部门信息
        List<Department> departments = departmentMapper.queryAllDepartment ();
        model.addAttribute ("departments",departments);
        return "emp/add";
    }
    @PostMapping("/emp")
    public String toEmp(Employee employee){
        //查出所有部门信息
        System.out.println (employee);
        employeeMapper.addEmployee (employee);//调用底层业务方法保存员工信息
        return "redirect:/emps";
    }
    /*去到员工的修改页面*/
    @GetMapping("/emp/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model){
        //查出原来的数据
        Employee employee=employeeMapper.queryEmployee (id);
        model.addAttribute ("emp",employee);
        List<Department> departments = departmentMapper.queryAllDepartment ();
        model.addAttribute ("departments",departments);
        return "/emp/update";
    }
    @PostMapping("/updateEmp")
    public String updateEmp(Employee employee)
    {
        employeeMapper.updateEmployee (employee);
        return "redirect:/emps";
    }
    @GetMapping("/delemp/{id}")
    public String toDelete(@PathVariable("id") Integer id){
        //查出原来的数据
        employeeMapper.deleteEmployee (id);
        return "redirect:/emps";
    }
}
